package com.chif.goingplus.aop.log.service;

import com.alibaba.fastjson.JSON;
import com.chif.goingplus.aop.log.IPUtil;
import com.chif.goingplus.aop.log.mapper.LogNoteMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liudachu
 * @description: 日志记录业务类
 * @date 2021/1/18
 */
@Service
@Slf4j
public class LogNoteService {

    @Resource
    public LogNoteMapper logNoteMapper;

    /**
     * 日志插入(后期可做异步处理)
     *
     * @param joinPoint
     * @param data
     * @param logNote
     */
    public void logInsert(JoinPoint joinPoint, Object data, com.chif.goingplus.aop.log.pojo.LogNote logNote) {
        logNote.setInterfaceUrl(getUrl(joinPoint));
        logNote.setInterfaceRequest(getRequestContent(joinPoint.getArgs()));
        logNote.setInterfaceReponseTime(System.currentTimeMillis());
        logNote.setInterfaceReponse(data == null ? "" : JSON.toJSONString(data));
        logNote.setInterfaceLengthTime(logNote.getInterfaceReponseTime() - logNote.getInterfaceRequestTime());
        logNote.setInterfaceIp(IPUtil.getLocalIP());
        logNoteMapper.insertLogNote(logNote);
    }

    /**
     * 获取访问地址
     *
     * @param joinPoint
     * @return
     */
    private String getUrl(JoinPoint joinPoint) {
        StringBuffer url = new StringBuffer();
        Method methodSignature = ((MethodSignature) joinPoint.getSignature()).getMethod();
        Object target = joinPoint.getTarget();
        methodSignature.getParameters();
        RequestMapping requestMapping = target.getClass().getAnnotation(RequestMapping.class);
        if (requestMapping != null && requestMapping.value().length > 0) {
            url.append(requestMapping.value()[0]);
        }
        PostMapping postMapping = methodSignature.getAnnotation(PostMapping.class);
        if (postMapping != null && postMapping.value().length > 0) {
            url.append(postMapping.value()[0]);
        }
        GetMapping getMapping = methodSignature.getAnnotation(GetMapping.class);
        if (getMapping != null && getMapping.value().length > 0) {
            url.append(getMapping.value()[0]);
        }
        RequestMapping request = methodSignature.getAnnotation(RequestMapping.class);
        if (request != null && request.value().length > 0) {
            url.append(request.value()[0]);
        }
        DeleteMapping deleteMapping = methodSignature.getAnnotation(DeleteMapping.class);
        if (deleteMapping != null && deleteMapping.value().length > 0) {
            url.append(deleteMapping.value()[0]);
        }
        PutMapping putMapping = methodSignature.getAnnotation(PutMapping.class);
        if (putMapping != null && putMapping.value().length > 0) {
            url.append(putMapping.value()[0]);
        }
        return url.toString();
    }

    /**
     * 获取请求内容
     *
     * @param objects
     * @return
     */
    private String getRequestContent(Object[] objects) {
        StringBuffer stringBuffer = new StringBuffer();
        AtomicInteger i = new AtomicInteger(1);
        Arrays.stream(objects).forEach(item -> {
            if (!Objects.isNull(item) && objects.length > 1) {
                stringBuffer.append("参数" + (i.getAndIncrement()) + ":");
            }
            stringBuffer.append(JSON.toJSONString(item));
        });
        return stringBuffer.toString();
    }


}
